#!/usr/bin/env bash
set -e
if [[ -z "$1" ]]; then
  echo 'Usage '$0' [path-to-project]'
  exit 1
fi

if [[ ! -d "$1" ]]; then
  echo "Path '$1' does not exist or its not a directory"
  exit 1
fi

PROJECT_PATH=$(cd `dirname $1` && pwd)
PROJECT_NAME=${PROJECT_PATH##*\/}
ADDON_PATH=$(cd `dirname $0` && pwd)
cp -r $ADDON_PATH/skel/* $1/



sed  -i.bak '/^  env\-storage\:[^$]*$/a\
  db-storage:'$'\n' "$1/docker/containers/compose.yml" && rm -f "$_.bak"

db_revision='10.5.9-a'
echo 'db[docker/containers/db]: -t ${COMPOSE_PROJECT_NAME}/db:'$db_revision >> "$1/docker/Buildfile"

db_name=$PROJECT_NAME
db_user=$PROJECT_NAME
db_user_pw=$(pwgen -Cs 20 1 | tr -d ' ' | tr -d '\n')
db_root_pw=$(pwgen -Cs 20 1 | tr -d ' ' | tr -d '\n')
echo "
export DB_IP=0.0.0.0 \\
  DB_NAME=$db_name \\
  DB_USER=$db_user \\
  DB_PASSWORD=$db_user_pw \\
  DB_ROOT_PASSWORD=$db_root_pw \\
  DB_REVISION=$db_revision \\
  DB_PORT=3306
" >> "$1/docker/env.sh"


echo "
mysql:
  connect_timeout: 1
  shard:
    - 'mysql:host=db;port=3306;dbname=$db_name;user=$db_user;password=$db_user_pw'

mysql:production:
  shard:
    - 'mysql:host=10.0.0.1;port=3306;dbname=$db_name;user=$db_user;password=production_pw'
" >> "$1/app/config/app.yml.tpl"
